package com.sense.sensedialprehandle.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * User: yangyuchang
 * Date: 2018/12/27
 * Time: 09:46
 */
public class DateUtils {

    //随机数生成器
    private static Random random = new Random();
    //日期格式
    private static SimpleDateFormat DATEFORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
    private static SimpleDateFormat date_Format = new SimpleDateFormat("yyyy-MM-dd");
    private static SimpleDateFormat date_Format_zero = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
    private static SimpleDateFormat date_Format_night_zero = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
    private static SimpleDateFormat dateFormats = new SimpleDateFormat("yyyyMM");
    private static DateTimeFormatter timeFormatters = DateTimeFormatter.ofPattern("yyyyMM");
    private static SimpleDateFormat format = new SimpleDateFormat("MMdd");
    private static SimpleDateFormat format1 = new SimpleDateFormat("yyyyMMdd");


    /**
     * 判断该日期是否是该月的第一天
     *
     * @param date 需要判断的日期
     * @return
     */
    public static boolean isFirstDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        System.out.println(calendar.get(Calendar.MONTH));
        return calendar.get(Calendar.DAY_OF_MONTH) == 1;
    }

    /**
     * 判断该日期是否是该月的最后一天
     *
     * @param date 需要判断的日期
     * @return
     */
    public static boolean isLastDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_MONTH) == calendar
                .getActualMaximum(Calendar.DAY_OF_MONTH);
    }


    /**
     * 判断是否是周末
     *
     * @return
     */
    public static boolean isWeekend(Date bdate) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(bdate);
        if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
            return true;
        } else {
            return false;
        }

    }

    /**
     * 判断是否是周日
     *
     * @return
     */
    public static boolean isSunday(Date bdate) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(bdate);
        if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
            return true;
        } else {
            return false;
        }

    }

    /**
     * 获得上个月月份年份
     */
    public static String getYearMonth() {
        Calendar now = Calendar.getInstance();
        int month = now.get(Calendar.MONTH);
        String m;
        if (month < 10) {
            m = "0" + month;
        } else {
            m = month + "";
        }
        return now.get(Calendar.YEAR) + m;
    }

    /**
     * 获得上个月月份年份
     * java8限定
     */
    public static String getLastMonth() {
        LocalDate today = LocalDate.now();
        today = today.minusMonths(1);

        return timeFormatters.format(today);
    }

    /**
     * 获得昨天
     */
    public static Date getYesterday() {

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, -24);
       // String yesterdayDate = date_Format.format(calendar.getTime());
        return calendar.getTime();
    }

    /**
     * 获得昨天的年月
     */
    public static String getYesterYM() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, -24);
        String yesterdayYM = dateFormats.format(calendar.getTime());
        return yesterdayYM;
    }

      // 获得当前时间
       public static String getNowDay() {
         Calendar calendarEnd = Calendar.getInstance();
         return format1.format(calendarEnd.getTime());
        }

    // 获得当前时间
    public static String getNowTime() {
        Calendar calendarEnd = Calendar.getInstance();
        return DATEFORMAT.format(calendarEnd.getTime());
    }

    // 获得某天最大时间 2018-12-27 23:59:59
    public static String getEndOfDay(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
        LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX);
        return DATEFORMAT.format(Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant()));
    }

    // 获得某天最小时间 2018-12-27 00:00:00
    public static String getStartOfDay(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
        LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN);
        return DATEFORMAT.format(Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant()));

    }

    //月的起始时间"yyyy-MM-dd HH:mm:ss"
    public static String getStartOfMonth(String date) throws ParseException {
        Calendar c=Calendar.getInstance();
        c.setTime(dateFormats.parse(date));
        int lastMonthMaxDay=c.getActualMaximum(Calendar.DAY_OF_MONTH);
        c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), lastMonthMaxDay, 23, 59, 59);

        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-01 00:00:00");
        String gtime = sdf2.format(c.getTime()); //月第一天

        return gtime;
    }

    //月的结束时间"yyyy-MM-dd HH:mm:ss"
    public static String getEndOfMonth(String date) throws ParseException {
        Calendar c=Calendar.getInstance();
        c.setTime(dateFormats.parse(date));
        int lastMonthMaxDay=c.getActualMaximum(Calendar.DAY_OF_MONTH);
        c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), lastMonthMaxDay, 23, 59, 59);

        //按格式输出
        String gtime = DATEFORMAT.format(c.getTime()); //月最后一天
        return gtime;
    }
    //判断当前时间是否是最近七天
    public static boolean isLatestWeek(Date addtime){
        Calendar calendar = Calendar.getInstance();  //得到日历
        calendar.setTime(new Date());//把当前时间赋给日历
        calendar.add(Calendar.DAY_OF_MONTH, -7);  //设置为7天前
        Date before7days = calendar.getTime();   //得到7天前的时间
        if(before7days.getTime() <= addtime.getTime()){
            return true;
        }else{
            return false;
        }
    }

    public static Map putDefaultDateTimeToMap() {
        Map<String,Object> map=new HashMap();
        map.put("startDateTime",getStartOfDay(new Date()));
        map.put("endDateTime",getEndOfDay(new Date()));
        return map;
    }

    public static Map putSetDateTimeToMap(int year,int month,int day) {
        LocalDate dateStart = LocalDate.of(year, month, day);
        LocalDate dateEnd = LocalDate.of(year, month, day);
        Map<String,Object> map=new HashMap();
        map.put("startDateTime",dateStart.toString()+" 00:00:00");
        map.put("endDateTime",dateEnd.toString()+" 23:59:59");
        return map;
    }


    public static String timeStamp2Date(String seconds,String format) {
        if(seconds == null || seconds.isEmpty() || seconds.equals("null")){
            return "";
        }
        if(format == null || format.isEmpty()){
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds)));
    }




    public static void main(String[] args) {
        System.out.println(timeStamp2Date("1578642746414",null));
    }

}
